﻿Imports System.ComponentModel
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Drawing

Namespace Controls
    ''' <summary> 
    ''' 日历控件。 
    ''' </summary> 
    <DefaultProperty("ScriptPath"), ToolboxData("<{0}:Calendar runat=server></{0}:Calendar>")> _
    Public Class Calendar
        Inherits WebControl
        Implements IPostBackDataHandler
        Implements INamingContainer
        '为子控件提供了一个新的命名范围,保证子控件的ID唯一性 
        Protected DateTextBox As New TextBox()
        Protected ImgHtmlImage As New System.Web.UI.HtmlControls.HtmlImage()

        ''' <summary> 
        ''' 构造函数 
        ''' </summary> 
        Public Sub New()
            MyBase.New()
        End Sub

        ''' <summary> 
        ''' 创建子控件 
        ''' </summary> 
        Protected Overloads Overrides Sub CreateChildControls()
            DateTextBox.Size = 8
            DateTextBox.ID = Me.ID
            Me.Controls.Add(DateTextBox)

            ImgHtmlImage.Src = ImageUrl
            ImgHtmlImage.Align = "bottom"
            ImgHtmlImage.Attributes.Add("onclick", "showcalendar(event, $('" + Me.ID + "_" + Me.ID + "'))")
            ImgHtmlImage.Attributes.Add("class", "calendarimg")
            Me.Controls.Add(ImgHtmlImage)

            Dim RegularExpressionValidator1 As System.Web.UI.WebControls.RegularExpressionValidator = New RegularExpressionValidator()
            RegularExpressionValidator1.ID = RegularExpressionValidator1.ClientID
            RegularExpressionValidator1.Display = System.Web.UI.WebControls.ValidatorDisplay.Dynamic
            RegularExpressionValidator1.ControlToValidate = DateTextBox.ID
            RegularExpressionValidator1.ValidationExpression = "^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$"
            RegularExpressionValidator1.ErrorMessage = "请输入正确的日期,如:2006-1-1"
            Me.Controls.Add(RegularExpressionValidator1)

            MyBase.CreateChildControls()
        End Sub

        ''' <summary> 
        ''' 添加属性方法 
        ''' </summary> 
        ''' <param name="key">键值,如class等</param> 
        ''' <param name="valuestr">要绑定的字符串</param> 
        Public Sub AddAttributes(ByVal key As String, ByVal valuestr As String)
            DateTextBox.Attributes.Add(key, valuestr)
        End Sub



#Region "protected override void OnPreRender(EventArgs e)"
        ''' <summary> 
        ''' 重写<see cref="System.Web.UI.Control.OnPreRender"/>方法。 
        ''' </summary> 
        ''' <param name="e">包含事件数据的 <see cref="EventArgs"/> 对象。</param> 
        Protected Overloads Overrides Sub OnPreRender(ByVal e As EventArgs)
#If NET1 Then
        If Not Page.IsClientScriptBlockRegistered("CalendarSet") Then 
            Page.RegisterClientScriptBlock("CalendarSet", String.Format("<SCRIPT language='javascript' src='{0}'></SCRIPT>", ScriptPath)) 
        End If 
#Else
            If Not Page.ClientScript.IsClientScriptBlockRegistered("CalendarSet") Then
                Page.ClientScript.RegisterClientScriptBlock(Me.[GetType](), "CalendarSet", String.Format("<SCRIPT language='javascript' src='{0}'></SCRIPT>", ScriptPath))
            End If
#End If

            MyBase.OnPreRender(e)
        End Sub

#End Region


#Region "Property ImageUrl"

        ''' <summary> 
        ''' 图片的路径 
        ''' </summary> 
        <Bindable(True), Category("Appearance"), DefaultValue("")> _
        Public Property ImageUrl() As String
            Get
                If MyBase.ViewState("imageurl") IsNot Nothing Then
                    Return DirectCast(MyBase.ViewState("imageurl"), String)
                Else
                    Return "../images/btn_calendar.gif"
                End If
            End Get
            Set(ByVal value As String)
                MyBase.ViewState("imageurl") = value
                ImgHtmlImage.Src = value
            End Set
        End Property

#End Region


#Region "Property Date"

        ''' <summary> 
        ''' 当前选择的日期 
        ''' </summary> 
        <Description("当前选择的日期。"), DefaultValue("")> _
        Public Property SelectedDate() As DateTime
            Get
                Try
                    Return DateTime.Parse(DateTextBox.Text)
                Catch
                    Return Convert.ToDateTime("1900-1-1")
                End Try
            End Get
            Set(ByVal value As DateTime)
                Try
                    DateTextBox.Text = value.ToString("yyyy-MM-dd")
                Catch
                    DateTextBox.Text = ""
                End Try
            End Set
        End Property

#End Region


#Region "Property ReadOnly"

        ''' <summary> 
        ''' 是否是只读。 
        ''' </summary> 
        <Description("是否是只读。"), DefaultValue(True)> _
        Public Property [ReadOnly]() As Boolean
            Get
                If Environment.Version.Major = 1 Then
                    Return DateTextBox.[ReadOnly]
                Else
                    Return False
                End If
            End Get
            Set(ByVal value As Boolean)
                If Environment.Version.Major = 1 Then
                    DateTextBox.[ReadOnly] = value
                End If
            End Set
        End Property

#End Region


#Region "Property ScriptPath"

        ''' <summary> 
        ''' Javascript脚本文件所在目录。 
        ''' </summary> 
        <Description("Javascript脚本文件所在目录。"), DefaultValue("./")> _
        Public Property ScriptPath() As String
            Get
                Dim obj As Object = ViewState("ScriptPath")
                Return IIf(obj Is Nothing, "../js/calendar.js", DirectCast(obj, String))
            End Get
            Set(ByVal value As String)
                ViewState("ScriptPath") = value
            End Set
        End Property

#End Region


#Region "Property BorderStyle"

        ''' <summary> 
        ''' 边框的样式。 
        ''' </summary> 
        <Description("边框的样式。"), Category("Appearance"), DefaultValue(""), TypeConverter(GetType(BorderStyleConverter))> _
        Public Shadows Property BorderStyle() As String
            Get
                Dim obj As Object = ViewState("BorderStyle")
                Return IIf(obj Is Nothing, "solid", DirectCast(obj, String))
            End Get
            Set(ByVal value As String)
                ViewState("BorderStyle") = value
            End Set
        End Property

#End Region


#Region "Property BorderWidth"

        ''' <summary> 
        ''' 边框的宽度。 
        ''' </summary> 
        <Description("边框的宽度。"), Category("Appearance"), DefaultValue("")> _
        Public Shadows Property BorderWidth() As String
            Get

                Dim obj As Object = ViewState("BorderWidth")
                Return IIf(obj Is Nothing, "1", DirectCast(obj, String))
            End Get
            Set(ByVal value As String)
                ViewState("BorderWidth") = value
            End Set
        End Property

#End Region


#Region "Property BorderColor"

        ''' <summary> 
        ''' 边框的颜色。 
        ''' </summary> 
        <Description("边框的颜色。"), Category("Appearance"), DefaultValue("")> _
        Public Overloads Overrides Property BorderColor() As Color
            Get
                Dim obj As Object = ViewState("BorderColor")
                Return IIf(obj Is Nothing, Color.FromArgb(153, 153, 153), DirectCast(obj, Color))
            End Get
            Set(ByVal value As Color)
                ViewState("BorderColor") = value
            End Set
        End Property
#End Region

#Region "IPostBackDataHandler 成员"

        ''' <summary> 
        ''' 引发回传事件 
        ''' </summary> 
        Public Sub RaisePostDataChangedEvent() Implements IPostBackDataHandler.RaisePostDataChangedEvent
        End Sub

        ''' <summary> 
        ''' 加载提交信息 
        ''' </summary> 
        ''' <param name="postDataKey"></param> 
        ''' <param name="postCollection"></param> 
        ''' <returns></returns> 
        Public Function LoadPostData(ByVal postDataKey As String, ByVal postCollection As System.Collections.Specialized.NameValueCollection) As Boolean Implements IPostBackDataHandler.LoadPostData
            Dim presentValue As String = Me.DateTextBox.Text
            Dim postedValue As String = postCollection(postDataKey)

            If Not presentValue.Equals(postedValue) Then
                '如果回发数据不等于原有数据 
                Me.DateTextBox.Text = postedValue
                Return True
            End If
            Return False

        End Function
#End Region


        ''' <summary> 
        ''' 输出html,在浏览器中显示控件 
        ''' </summary> 
        ''' <param name="output"> 要写出到的 HTML 编写器 </param> 
        Protected Overloads Overrides Sub Render(ByVal output As HtmlTextWriter)

            If Me.HintInfo <> "" Then
                output.WriteBeginTag("span id=""" + Me.ClientID + """ onmouseover=""showhintinfo(this," + Me.HintLeftOffSet + "," + Me.HintTopOffSet + ",'" + Me.HintTitle + "','" + Me.HintInfo + "','" + Me.HintHeight + "','" + Me.HintShowType + "');"" onmouseout=""hidehintinfo();"">")
            End If

            RenderChildren(output)

            If Me.HintInfo <> "" Then
                output.WriteEndTag("span")
            End If
        End Sub

    End Class
End Namespace